#pragma once

#include <cstdint>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

#define PROTOCOL_BUFFER_LEN    2048
#define PROTOCOL_CRC_LEN  2

#define PROTOCOL_HEAD1    0x8f
#define PROTOCOL_HEAD2    0x70

enum
{
	STEP_HEAD1,
	STEP_HEAD2,
	STEP_CMD,
	STEP_LEN,
	STEP_DATA,
	STEP_VERIFY,
};

#pragma pack(1)
typedef struct
{
	uint8_t head1;
	uint8_t head2;
	uint8_t cmd;
	uint16_t len;
	uint8_t* data;
	uint16_t crc;
}protocol_t;

typedef union
{
	uint8_t buff[sizeof(protocol_t) + PROTOCOL_BUFFER_LEN + PROTOCOL_CRC_LEN];
	protocol_t pack;
}protocol_data_t;
#pragma pack()

class Protocol
{
public:
	Protocol();
	~Protocol();
	protocol_data_t* parse(uint8_t data, protocol_t* p);
	uint16_t load(uint8_t* buf, uint8_t cmd, uint8_t* data = NULL, uint16_t len = 0);

private:
	protocol_data_t ptl;
	uint8_t step;
	uint16_t cnt;
};
